<?php
require_once '../../../../../config.php';
require_once EDITOR_INC.'/application/libraries/SyncFile.php';
//不限制响应时间
@set_time_limit(0);
//引入水印文件
require('../watermark/watermark.class.php');
/*
 * 上传函数
 *
 * @access         public
 * @upfile         string  上传表单name
 * @iswatermark    string  是否添加水印
 */
function UploadFile($upfile, $iswatermark=''){
    $cfg_max_file_size = '2097152';
    //图片文件上传地址
	$cfg_image_dir = EDITOR_UPLOAD.'/forum';
	$cfg_soft_dir  = EDITOR_UPLOAD.'/forum';
	$cfg_media_dir = EDITOR_UPLOAD.'/forum';
    $cfg_upload_img_type   = 'gif|png|jpg|jpeg|bmp';
    $cfg_upload_soft_type  = 'zip|gz|rar|iso|doc|xls|ppt|wps|txt'; 
    $cfg_upload_media_type = 'swf|flv|mpg|mp3|rm|rmvb|wmv|wma|wav';
	global $cfg_markswitch, $cfg_markpicurl, $cfg_markminwidth, $cfg_markminheight, $cfg_markwhere, $cfg_marktext, $cfg_marksize, $cfg_markcolor, $cfg_marktype;

	//检测是否存在
	$tempfile_tn = isset($_FILES[$upfile]['tmp_name']) ? $_FILES[$upfile]['tmp_name'] : '';
    if($tempfile_tn == '' or !is_uploaded_file($tempfile_tn)){
		return '请选择上传文件或您上传的文件超过php.ini设定最大文件上传限制['.ini_get('upload_max_filesize').']！';
	}
	
	//获取上传文件信息
	$tempfile      = $_FILES[$upfile];
	$tempfile_name = $tempfile['name'];
	$tempfile_size = $tempfile['size'];
	$tempfile_ext  = strtolower(substr(strrchr($tempfile_name, '.'), 1));

	//强制限定的某些文件类型禁止上传
	if(in_array($tempfile_ext, explode('|', 'php|pl|cgi|asp|aspx|jsp|php3|shtm|shtml'))){
		return '您上传的文件类型为：['.$tempfile_ext.']，该类文件不允许通过后台上传！';
	}
	//检查文件类型,上传文件目录
	if(in_array($tempfile_ext, explode('|', strtolower($cfg_upload_img_type)))){
		$upload_url = 'forum';
		$upload_dir = $cfg_image_dir;
	}else if(in_array($tempfile_ext, explode('|', strtolower($cfg_upload_soft_type)))){
		$upload_url = 'forum';
		$upload_dir = $cfg_soft_dir;
	}else if(in_array($tempfile_ext, explode('|', strtolower($cfg_upload_media_type)))){
		$upload_url = 'forum';
		$upload_dir = $cfg_media_dir;
	}else{
		return '您上传的文件类型为：['.$tempfile_ext.']，该文件类型不允许上传！';
	}
	$save_type = $upload_url;

	//检查文件大小
	if($tempfile_size > $cfg_max_file_size){
		return '您上传的文件超过系统设定最大文件上传限制['.GetRealSize($cfg_max_file_size).']！';
	}
	//创建文件夹
	if(!file_exists($upload_dir)){
		mkdir($upload_dir);
	}
	//检查目录可写权限
	if(@!is_writable($upload_dir)){
		return '上传目录没有可写权限！';
	}
	$ym = date('Ym');
	$upload_url .= '/'.$ym.'/'.date('d');
	$upload_dir .= '/'.$ym.'/'.date('d');

	if(!file_exists($upload_dir)){
        mkdir($upload_dir,0777,true);
		$fp = fopen($upload_dir.'/index.htm', 'w');
		fclose($fp);
	}
	//上传文件名称
	$filename = time()+rand(1,9999).'.'.$tempfile_ext;
	//上传文件路径
	$save_url = 'data/'.$upload_url.'/'.$filename;
	$save_dir = $upload_dir.'/'.$filename;

	if(file_exists($save_dir)){
		return '同名文件已经存在了！';
	}
	//移动临时文件到指定目录
	//$save_dir
	if(move_uploaded_file($_FILES[$upfile]['tmp_name'], $save_dir)){
		//加水印的效果
		if($cfg_markswitch=='Y' && ($iswatermark=='true' or  $iswatermark=='checked') && 
			in_array($tempfile_ext, array('jpg','jpeg','png','gif','bmp'))){
				
			WaterMark($save_dir, EDITOR_INC.'/'.$cfg_markpicurl,
					  $cfg_markminwidth, $cfg_markminheight, $cfg_markwhere,
			          $cfg_marktext, '黑体', $cfg_marksize, $cfg_markcolor, $cfg_marktype);
		}
        //同步到阿里云对象存储服务
        $syncFile = new SyncFile();
        $syncFile->uploadFile($upload_url.'/'.$filename, EDITOR_UPLOAD.'/'.$upload_url.'/'.$filename);

		//上传成功，返回数组
		return array($filename, $tempfile_size, $save_url, $save_dir);
	}else{
		return '发生未知错误，上传失败！';
	}
}
?>